//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PiecewiseZeroSpreadedTermStructure.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Yield/ZeroYieldStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::IVector<Cephei::QL::IQuote^>^ spreads, Cephei::IVector<DateTime>^ dates) : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
    CYieldTermStructure^ _Cprm1;
    CoVector<Cephei::QL::IQuote^>^ _Cspreads;
    CoVector<DateTime>^ _Cdates;
    try
    {
#ifdef HANDLE
        _phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
        _Cprm1 = safe_cast<CYieldTermStructure^> (prm1);
        _Cprm1->Lock();
        Handle<QuantLib::YieldTermStructure>& _prm1 = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cprm1->GetHandle ()); 
        _Cspreads = safe_cast<CoVector<Cephei::QL::IQuote^>^> (spreads);
        _Cspreads ->Lock ();
        INativeVector<Cephei::QL::IQuote^>^ _NCIspreads = _Cspreads->getFeature (NativeFeature::Handle);
        CQuoteVector^ _NCspreads = safe_cast<CQuoteVector^>(_NCIspreads);
        std::vector<Handle<QuantLib::Quote> >& _spreads = static_cast<std::vector<Handle<QuantLib::Quote> >&> (_NCspreads->GetHandle ());
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        _ppPiecewiseZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure> (new QuantLib::PiecewiseZeroSpreadedTermStructure ( _prm1,  _spreads,  _dates ));
        SetZeroYieldStructure (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cspreads != nullptr) _Cspreads->Unlock();
        if (_Cdates != nullptr) _Cdates->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure>& childNative, Object^ owner) : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
	_ppPiecewiseZeroSpreadedTermStructure = &childNative;
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
}
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (QuantLib::PiecewiseZeroSpreadedTermStructure& childNative, Object^ owner) : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
	_ppPiecewiseZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure> (&childNative);
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
    _PiecewiseZeroSpreadedTermStructureOwner = owner;
    _ZeroYieldStructureOwner = owner;
}

Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (CPiecewiseZeroSpreadedTermStructure^ copy) : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPiecewiseZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure> (copy->GetShared());
        _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
    }
}
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (System::Type^ t) : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CPiecewiseZeroSpreadedTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (QuantLib::Handle<QuantLib::PiecewiseZeroSpreadedTermStructure>& childNative, Object^ owner)  : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
	_phPiecewiseZeroSpreadedTermStructure = &childNative;
	_ppPiecewiseZeroSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
    _PiecewiseZeroSpreadedTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (QuantLib::Handle<QuantLib::PiecewiseZeroSpreadedTermStructure> childNative)  : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
	_phPiecewiseZeroSpreadedTermStructure = &childNative;
	_ppPiecewiseZeroSpreadedTermStructure = &static_cast<boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure>>(childNative.currentLink());
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::CPiecewiseZeroSpreadedTermStructure (QuantLib::PiecewiseZeroSpreadedTermStructure childNative)  : CZeroYieldStructure(CPiecewiseZeroSpreadedTermStructure::typeid)
{
#ifdef HANDLE
	_phPiecewiseZeroSpreadedTermStructure = NULL;
#endif
	_ppPiecewiseZeroSpreadedTermStructure = new boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure> (new QuantLib::PiecewiseZeroSpreadedTermStructure (childNative));
    _ppZeroYieldStructure = new boost::shared_ptr<QuantLib::ZeroYieldStructure> (boost::dynamic_pointer_cast<QuantLib::ZeroYieldStructure> (*_ppPiecewiseZeroSpreadedTermStructure));
}
#endif

Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::~CPiecewiseZeroSpreadedTermStructure ()
{
    if (_ppPiecewiseZeroSpreadedTermStructure != NULL)
    {
	    delete _ppPiecewiseZeroSpreadedTermStructure;
        _ppPiecewiseZeroSpreadedTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::!CPiecewiseZeroSpreadedTermStructure ()
{
    if (_ppPiecewiseZeroSpreadedTermStructure != NULL)
    {
	    delete _ppPiecewiseZeroSpreadedTermStructure;
    }
}
QuantLib::PiecewiseZeroSpreadedTermStructure& Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::GetReference ()
{
    if (_ppPiecewiseZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppPiecewiseZeroSpreadedTermStructure;
}
boost::shared_ptr<QuantLib::PiecewiseZeroSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::GetShared ()
{
    if (_ppPiecewiseZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppPiecewiseZeroSpreadedTermStructure;
}
QuantLib::PiecewiseZeroSpreadedTermStructure* Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::GetPointer ()
{
    if (_ppPiecewiseZeroSpreadedTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppPiecewiseZeroSpreadedTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PiecewiseZeroSpreadedTermStructure>& Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::GetHandle ()
{
	if (_phPiecewiseZeroSpreadedTermStructure == NULL)
	{
		_phPiecewiseZeroSpreadedTermStructure = new Handle<QuantLib::PiecewiseZeroSpreadedTermStructure> (*_ppPiecewiseZeroSpreadedTermStructure);
	}
	return *_phPiecewiseZeroSpreadedTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::HasNative () 
{
	return (_ppPiecewiseZeroSpreadedTermStructure != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppPiecewiseZeroSpreadedTermStructure)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppPiecewiseZeroSpreadedTermStructure)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppPiecewiseZeroSpreadedTermStructure)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppPiecewiseZeroSpreadedTermStructure)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppPiecewiseZeroSpreadedTermStructure)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IPiecewiseZeroSpreadedTermStructure^ Cephei::QL::Termstructures::Yield::CPiecewiseZeroSpreadedTermStructure_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::IVector<Cephei::QL::IQuote^>^ spreads, Cephei::IVector<DateTime>^ dates)
{
    return gcnew CPiecewiseZeroSpreadedTermStructure ( prm1,  spreads,  dates);
}
